package com.meiyu.live.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meiyu.live.R;

public class MSGView extends FrameLayout {

    private LinearLayout ll_loading;
    private TextView tv_msg_loading, tv_msg_error, tv_msg_empty;

    private String emptyStr="没有数据";
    private String loadingStr="别催，加载着呢~";
    private String errorStr="加载失败，点击重试";


    public MSGView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MSGView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MSGView(Context context) {
        super(context);
        init();
    }

    public void setEmptyStr(String emptyStr) {
        this.emptyStr = emptyStr;
    }

    public void setLoadingStr(String loadingStr) {
        this.loadingStr = loadingStr;
    }

    public void setErrorStr(String errorStr) {
        this.errorStr = errorStr;
    }

    private void init() {
        if (isInEditMode()) {
            return;
        }
        LayoutInflater.from(this.getContext()).inflate(R.layout.view_msg_layout, this);
        ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
        tv_msg_loading = (TextView) findViewById(R.id.tv_msg_loading);
        tv_msg_error = (TextView) findViewById(R.id.tv_msg_error);
        tv_msg_empty = (TextView) findViewById(R.id.tv_msg_empty);

        ll_loading.setVisibility(View.GONE);
        tv_msg_error.setVisibility(View.GONE);
        tv_msg_empty.setVisibility(View.GONE);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    private void setTextColor(TextView tv, int newColor) {
//        try {
//            int oldColor = tv.getCurrentTextColor();
//            ThemeChangeAnimationUtils.startThemeChangeAnimation(tv, "textColor", oldColor, newColor);
//        } catch (Exception e) {
            tv.setTextColor(newColor);
//        }
    }


    private void setBackgroundColor(View mView, int newColor) {
//        try {
//            int oldColor = 0;
//            Drawable d = mView.getBackground();
//            if (d instanceof ColorDrawable) {
//                oldColor = ((ColorDrawable) d).getColor();
//            }
//            ThemeChangeAnimationUtils.startThemeChangeAnimation(mView, "backgroundColor", oldColor, newColor);
//        } catch (Exception e) {
            mView.setBackgroundColor(newColor);
//        }
    }


    public void dismiss() {
        this.setVisibility(View.GONE);
    }


    public void showLoading() {
        showLoading(loadingStr);
    }


    public void showLoading(String loadingString) {
        if (!TextUtils.isEmpty(loadingString)) {
            tv_msg_loading.setText(loadingString);
        }
        this.setVisibility(View.VISIBLE);
        ll_loading.setVisibility(View.VISIBLE);
        tv_msg_error.setVisibility(View.GONE);
        tv_msg_empty.setVisibility(View.GONE);
    }


    public void show_empty() {
        show_empty(emptyStr);
    }


    public void show_empty(String emptyString) {
        if (!TextUtils.isEmpty(emptyString)) {
            tv_msg_empty.setText(emptyString);
        }
        this.setVisibility(View.VISIBLE);
        ll_loading.setVisibility(View.GONE);
        tv_msg_error.setVisibility(View.GONE);
        tv_msg_empty.setVisibility(View.VISIBLE);
    }

    public void show_error() {
        show_error(errorStr);
    }

    public void show_error(String errorString) {
        if (!TextUtils.isEmpty(errorString)) {
            tv_msg_error.setText(errorString);
        }
        this.setVisibility(View.VISIBLE);
        ll_loading.setVisibility(View.GONE);
        tv_msg_error.setVisibility(View.VISIBLE);
        tv_msg_empty.setVisibility(View.GONE);
    }

    public void setErrorClick(OnClickListener listener) {
        if (listener != null) {
            tv_msg_error.setOnClickListener(listener);
        }
    }

    public void setEmptyClick(OnClickListener listener) {
        if (listener != null) {
            tv_msg_empty.setOnClickListener(listener);
        }
    }

}
